package com.yra.service.impl;

import com.yra.domain.IndexConfig;
import com.yra.domain.Product;
import com.yra.mapper.IndexConfigMapper;
import com.yra.mapper.ProductMapper;
import com.yra.service.IndexConfigService;
import com.yra.util.PageQueryUtil;
import com.yra.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: yrq
 * @Date: Create in 13:41 2019/12/17
 */
@Service
public class IndexConfigServiceImpl implements IndexConfigService {
    @Autowired
    private IndexConfigMapper indexConfigMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageResult getConfigsPage(PageQueryUtil pageQueryUtil) {
        List<IndexConfig> configs=indexConfigMapper.getConfigPageList(pageQueryUtil);
        for (IndexConfig config:configs){
            Product product = productMapper.productById(config.getProductId());
            config.setProductName(product.getProductName());
            config.setProductImg(product.getProductImg());
        }
        int total=indexConfigMapper.getTotalConfig(pageQueryUtil);
        PageResult pageResult = new PageResult(configs, total, pageQueryUtil.getLimit(), pageQueryUtil.getPage());
        return pageResult;
    }

    @Override
    public List<Product> queryConfigForIndex(int type, int limit) {
        int[] ids = indexConfigMapper.queryProductIds(type, limit);
        if (ids.length>0){
            productMapper.queryProductByIds(ids);
            return productMapper.queryProductByIds(ids);
        }
        return null;
    }

    @Override
    public Boolean insertConfig(Map<String, Object> params) {
        return indexConfigMapper.insertConfig(params)>0;
    }

    @Override
    public Boolean updateConfig(Map<String, Object> params) {
        return indexConfigMapper.updateConfig(params)>0;
    }

    @Override
    public Boolean deleteConfigs(int[] ids) {
        return indexConfigMapper.deleteConfigs(ids)>0;
    }
}
